package com.code.zjm.activity;

import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.code.zjm.R;
import com.code.zjm.entity.User;
import com.code.zjm.helper.DBHelper;

/**
 * Created by ZJM on 2017/4/27.
 * 联系人详情界面
 */

public class EditUserActivity extends BaseUserActivity {
    private final static String TAG = EditUserActivity.class.getSimpleName();
    //编辑框对象
    private EditText etUsername, etCompany, etPosition, etMobilePhone, etFamilyPhone,
            etOfficePhone, etOtherContact, etEmail, etAddress, etRemark, etZipCode;
    //联系人对象
    private User user;
    //
    private boolean flag = false;
    private boolean isDataChanged = false;

    @Override
    public void initData() {
        user = intent.getParcelableExtra("user");
    }

    @Override
    protected void initView() {
        etUsername = (EditText) findViewById(R.id.et_username);
        etCompany = (EditText) findViewById(R.id.et_company);
        etPosition = (EditText) findViewById(R.id.et_position);
        etMobilePhone = (EditText) findViewById(R.id.et_mobile_phone);
        etFamilyPhone = (EditText) findViewById(R.id.et_family_phone);
        etOfficePhone = (EditText) findViewById(R.id.et_office_phone);
        etOtherContact = (EditText) findViewById(R.id.et_other_contact);
        etEmail = (EditText) findViewById(R.id.et_email);
        etAddress = (EditText) findViewById(R.id.et_address);
        etRemark = (EditText) findViewById(R.id.et_remark);
        etZipCode = (EditText) findViewById(R.id.et_zip_code);

        etUsername.setText(user.getUsername());
        etCompany.setText(user.getCompany());
        etPosition.setText(user.getPosition());
        etMobilePhone.setText(user.getMobilePhone());
        etFamilyPhone.setText(user.getFamilyPhone());
        etOfficePhone.setText(user.getOfficePhone());
        etOtherContact.setText(user.getOtherContact());
        etEmail.setText(user.getEmail());
        etAddress.setText(user.getAddress());
        etRemark.setText(user.getRemark());
        etZipCode.setText(user.getZipCode());

        userImage = (ImageView) findViewById(R.id.iv_user);
        userImage.setImageResource(user.getImageId());
        userImage.setOnClickListener(this);
    }

    @Override
    protected String getTAG() {
        return TAG;
    }

    @Override
    protected Object getContentView() {
        return R.layout.activity_edit;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //实例化解析菜单对象
        MenuInflater inflater = getMenuInflater();
        //解析菜单资源文件，保存在 menu 中
        inflater.inflate(R.menu.detail_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_ensure:
                //往数据库里面更新数据
                setTitle("modify");
                modify();
                setEditTextDisable();
                break;

            case android.R.id.home://对应 ActionBar 的返回图标
                if (isDataChanged) {
                    setResult(4);
                } else {
                    setResult(5);
                }
                finish();
                break;

            case R.id.action_edit:
                if (!flag) {
                    setEditTextEnable();
                    flag = true;
                } else {
                    setEditTextDisable();
                    flag = false;
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_user:
                loadImage();//为gallery装载图片
                initImageChooseDialog();//初始化imageChooseDialog
                imageChooseDialog.show();

        }
    }

    /**
     * 获得最新数据，创建DBHelper对象，更新数据库
     */
    private void modify() {
        if (user != null) {
            //判断姓名是否为空
            String name = etUsername.getText().toString();
            if (name.trim().equals("")) {
                Toast.makeText(EditUserActivity.this, "姓名不许为空", Toast.LENGTH_LONG).show();
                return;
            }

            user.setUsername(name);
            user.setCompany(etCompany.getText().toString());
            user.setPosition(etPosition.getText().toString());
            user.setMobilePhone(etMobilePhone.getText().toString());
            user.setFamilyPhone(etFamilyPhone.getText().toString());
            user.setOfficePhone(etOfficePhone.getText().toString());
            user.setOtherContact(etOtherContact.getText().toString());
            user.setEmail(etEmail.getText().toString());
            user.setAddress(etAddress.getText().toString());
            user.setRemark(etRemark.getText().toString());
            user.setZipCode(etZipCode.getText().toString());
            //判断头像是否改变，若改变，则用当前的位置，若没有改变，则用前一回的位置
            if (imageChanged) {
                user.setImageId(images[currentImagePosition % images.length]);
            } else {
                user.setImageId(images[previousImagePosition % images.length]);
            }
            user.setPrivacy(privacy);
            DBHelper helper = new DBHelper(this);
            //打开数据库
            helper.openDatabase();
            isDataChanged = helper.modify(user);
        }


    }

    /**
     * 设置EditText为不可用
     */
    private void setEditTextDisable() {
        etUsername.setEnabled(false);
        etMobilePhone.setEnabled(false);
        etOfficePhone.setEnabled(false);
        etFamilyPhone.setEnabled(false);
        etPosition.setEnabled(false);
        etCompany.setEnabled(false);
        etAddress.setEnabled(false);
        etZipCode.setEnabled(false);
        etOtherContact.setEnabled(false);
        etEmail.setEnabled(false);
        etRemark.setEnabled(false);
        userImage.setEnabled(false);
//        setColorToWhite();

    }

    /**
     * 设置EditText为可用状态
     */
    private void setEditTextEnable() {
        etUsername.setEnabled(true);
        etMobilePhone.setEnabled(true);
        etOfficePhone.setEnabled(true);
        etFamilyPhone.setEnabled(true);
        etPosition.setEnabled(true);
        etCompany.setEnabled(true);
        etAddress.setEnabled(true);
        etZipCode.setEnabled(true);
        etOtherContact.setEnabled(true);
        etEmail.setEnabled(true);
        etRemark.setEnabled(true);
        userImage.setEnabled(true);
//        setColorToBlack();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(getTAG(), "---------------onDestroy is called in " + getTAG());
        if (iSwitcher != null) {
            iSwitcher = null;
        }
        if (gallery != null) {
            gallery = null;
        }
        if (imageChooseDialog != null) {
            imageChooseDialog = null;
        }
        if (imageChooseView != null) {
            imageChooseView = null;
        }
        if (userImage != null) {
            userImage = null;
        }

    }
}
